<?php
class TBB_Utility_Checker
{	
	public static function isLoggedInUser()
	{
		$auth = Zend_Auth::getInstance();
		return $auth->hasIdentity() ? $auth->getIdentity() : null;
	}
	
	public static function isCustomer($identity)
	{
		$auth = Zend_Auth::getInstance();
		if($identity) {
			// get username			
			$identity = $auth->getIdentity();
			$username = $identity->username;
			
			// get user ID
			$userModel = new TBB_Model_Users();
			$userID = $userModel->getUserIDByUsername($username);
			
			// get user row
			$user = $userModel->find($userID)->current();
			
			// get roles of the user
			$roles = $userModel->getRolesOfUser($userID);
			
			// check if the user is customer
			$roleModel = new TBB_Model_Role();
			$customerRoleID = $roleModel->getCustomerRoleID();
			foreach($roles as $role) {
				if($role['role_id'] == $customerRoleID) {
					// return customer ID please
					return $user['customer_id'];
				}
			}		
			return null;
		}
		throw new Exception('There is no identity.');
	}
}